package com.itheima.health.service.impl;

import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.entity.Permission;
import com.itheima.health.pojo.entity.Role;
import com.itheima.health.pojo.entity.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        //调用DAO查询数据
        User user = userDao.selectByUsername(username);

        //根据用户id查询角色列表
        Set<Role> roles = roleDao.selectByUserId(user.getId());
        user.setRoles(roles);

        //根据角色id查询权限列表
        roles.forEach(role -> {
            Set<Permission> permissions = permissionDao.selectByRoleId(role.getId());
            role.setPermissions(permissions);
        });

        return user;
    }
}
